<template>
  <div>
    <el-form :model="formList" ref="form" :rules="rules" size="normal">
      <el-form-item label="项目名称" prop="project_name">
        <el-input v-model="formList.project_name" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="包组名称" prop="package_name">
        <el-input v-model="formList.package_name"></el-input>
      </el-form-item>
      <el-form-item label="序号" prop="sort">
        <el-input v-model="formList.sort"></el-input>
      </el-form-item>
    </el-form>
    <div class="center">
      <el-button type="danger" size="medium" @click="cancel">取消</el-button>
      <template v-if="this.switch">
        <el-button type="primary" size="medium" @click="onAdd" :loading="saveLoading" v-if="!formList.id">新增</el-button>
        <el-button type="primary" size="medium" @click="onEdit" :loading="saveLoading" v-else>修改</el-button>
      </template>
    </div>
  </div>
</template>
<script>
export default {
  name: 'BidProject',
  components: {

  },
  props: {
    switch: {
      type: Boolean,
      default: () => {
        return false
      }
    },
    formList: {
      type: Object,
      default: () => {
        return {}
      }
    },
  },
  data () {
    return {
      saveLoading: false,
      rules: {
        project_name: [
          { required: true, message: '请输入项目名称', trigger: 'blur' },
        ],
        package_name: [
          { required: true, message: '请输入包组名称', trigger: 'blur' },
        ],
        sort: [
          { required: false, validator: this.$util.isNum, trigger: 'blur' },
        ],
      }
    }
  },
  created () {

  },
  mounted () {
  },
  methods: {
    async onAdd () {
      this.saveLoading = true
      this.$refs['form'].validate(async (valid) => {
        if (valid) {
          const res = await this.$http('blockPackageAdd', {
            del_flag: '0',
            project_id: this.formList.project_id,
            sort: this.formList.sort,
            package_name: this.formList.package_name
          })
          this.$tips.success(res.errmsg)
          this.$emit('cancel')
          this.$emit('init')
          this.saveLoading = false
        } else {
          this.$tips.error('请检查必填项')
          this.saveLoading = false
          return false;
        }
      })
    },
    async onEdit () {
      this.saveLoading = true
      this.$refs['form'].validate(async (valid) => {
        if (valid) {
          const res = await this.$http('blockPackageUp', {
            del_flag: '0',
            id: this.formList.id,
            sort: this.formList.sort,
            package_name: this.formList.package_name
          })
          this.$tips.success(res.errmsg)
          this.$emit('cancel')
          this.$emit('init')
          this.saveLoading = false
        } else {
          this.$tips.error('请检查必填项')
          this.saveLoading = false
          return false;
        }
      })
    },
    cancel () {
      this.$emit('cancel')
    }
  },
  computed: {

  }
}
</script>
<style lang='scss' scoped>
</style>
